package com.zd.composedemo.ui.page

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.core.Animatable
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.graphics.toColorInt
import androidx.lifecycle.lifecycleScope
import com.zd.composedemo.MainActivity
import com.zd.composedemo.R
import com.zd.composedemo.ui.theme.ComposeDemoTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

class LoadingActivity : ComponentActivity() {
    private val stepT = 3
    private var timeC = mutableStateOf("${stepT}S跳过")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ComposeDemoTheme {
//                Box(modifier = Modifier.fillMaxSize()) {
//                    Image(
//                        painter = painterResource(R.drawable.img_start), contentDescription = null,
//                        modifier = Modifier.fillMaxSize(),
//                        contentScale = ContentScale.Crop
//                    )
//
//                    Scaffold(
//                        modifier = Modifier
//                            .fillMaxSize()
//                            .background(Color.Transparent)
//                    ) { inPadding ->
                        LoadingPage(timeC, modifier = Modifier.padding()) {
                            startActivity(Intent(this@LoadingActivity, MainActivity::class.java))
                            finish()
                        }
//                    }
//                }
//                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->


//                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        lifecycleScope.launch {
            delay(1000)
            repeat(stepT) {
                delay(1000)
                val stepOnly = stepT - (it + 1)
                if (stepOnly > 0) {
                    timeC.value = "${stepOnly}s跳过"
                } else {
                    timeC.value = "进入"
                }
            }
        }
    }
}

@Preview
@Composable
fun LoadingPage(
    timeStr: MutableState<String> = mutableStateOf("进入"),
    modifier: Modifier = Modifier,
    callIn: (() -> Unit)?
) {
    Box(
        modifier = modifier
            .fillMaxSize()
            .background(Color("#CCCCCC".toColorInt()))
    ) {
//        Image(
//            painter = painterResource(R.drawable.img_start),
//            contentDescription = null,
//            contentScale = ContentScale.Crop,
//            modifier = Modifier.fillMaxSize()
//        )

        Surface(
            modifier = Modifier
                .clickable { callIn?.invoke() }
                .padding(10.dp, 40.dp)
                .clip(RoundedCornerShape(8.dp))
                .align(Alignment.TopEnd)
        ) {

//            LaunchedEffect(timeStr) {
//                withContext(Dispatchers.Default){
//
//                }
//            }
            Text(
                text = timeStr.value,
                fontSize = 20.sp,
                color = Color.Red,
                modifier = Modifier
                    .padding(10.dp, 5.dp)
            )

            MyScreen()
        }

    }
}


@Composable
fun MyScreen() {
// [START android_compose_side_effects_launchedeffect]
    // Allow the pulse rate to be configured, so it can be sped up if the user is running
    // out of time
    var pulseRateMs by remember { mutableStateOf(3000L) }
    val alpha = remember { Animatable(1f) }
    LaunchedEffect(pulseRateMs) { // Restart the effect when the pulse rate changes
        while (isActive) {
            delay(pulseRateMs) // Pulse the alpha every pulseRateMs to alert the user
            alpha.animateTo(0f)
            alpha.animateTo(1f)
        }
    }
// [END android_compose_side_effects_launchedeffect]
}